import 'dart:math';
import 'package:blocks_blkk/event/event_bus.dart';
import 'package:flutter/material.dart';

typedef ChangePosition = void Function({required double x, required double y});
typedef RecoverPosition = bool Function();

enum SteerMoveEnum { Horizontal, Vertical, Both, None }

class Steer extends StatefulWidget {
  Steer(
      {super.key,
      required this.changeValue,
      required this.backgroundImg,
      required this.controlImg,
      this.radio = 0.3,
      this.direction = SteerMoveEnum.Both,
      this.left,
      this.top,
      this.right,
      this.bottom,
      this.width,
      this.height,
      this.lessImage,
      this.greatImage,
      this.recoverFunction});

  final ChangePosition changeValue;
  final String backgroundImg;
  final String controlImg;
  final double radio;
  final SteerMoveEnum direction; //拖动方向
  final left;
  final top;
  final right;
  final bottom;
  final width;
  final height;
  String? lessImage;
  String? greatImage;
  RecoverPosition? recoverFunction;
  // final String rightImage;
  // final String leftImage;

  @override
  SteerState createState() => SteerState();
}

class SteerState extends State<Steer> {
  double _alightX = 0.0; //距顶部的偏移
  double _alightY = 0.0;

  @override
  void initState() {
    super.initState();
    EventBus.instance.on('SteerReset', (arg) {
      debugPrint('SteerReset');
      _moveTo(0, 0);
    });
  }

  @override
  void dispose() {
    EventBus.instance.off('SteerReset');
    super.dispose();
  }

  //距左边的偏移
  double get _parentW => widget.width;

  String get _backgroundImage {
    // print('_backgroundImage ${widget.direction} $_alightX $_alightY');
    if (widget.direction == SteerMoveEnum.Both) {
      return widget.backgroundImg;
    }
    if (widget.direction == SteerMoveEnum.Vertical) {
      if (_alightY < 0) {
        return widget.lessImage ?? widget.backgroundImg;
      } else if (_alightY > 0) {
        return widget.greatImage ?? widget.backgroundImg;
      } else {
        return widget.backgroundImg;
      }
    } else {
      if (_alightX < 0) {
        return widget.lessImage ?? widget.backgroundImg;
      } else if (_alightX > 0) {
        return widget.greatImage ?? widget.backgroundImg;
      } else {
        return widget.backgroundImg;
      }
    }
  }

  void _moveTo(double px, double py) {
    if (widget.radio > 0.9) {
      return;
    }
    double dx = 0;
    double dy = 0;
    if (widget.direction == SteerMoveEnum.Horizontal ||
        widget.direction == SteerMoveEnum.Both) {
      dx = px;
    }
    if (widget.direction == SteerMoveEnum.Vertical ||
        widget.direction == SteerMoveEnum.Both) {
      dy = py;
    }

    if ((pow(dx, 2) + pow(dy, 2)) > pow(_parentW * (1 - widget.radio) / 2, 2)) {
      final double radians = atan2(dy, dx);
      dx = cos(radians) * _parentW * (1 - widget.radio) / 2;
      dy = sin(radians) * _parentW * (1 - widget.radio) / 2;
    }

    //发送命令
    widget.changeValue(
        x: 2 * dx / (_parentW * (1 - widget.radio)),
        y: 2 * dy / (_parentW * (1 - widget.radio)));

    setState(() {
      _alightX = dx;
      _alightY = dy;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // color: Colors.yellow,
      width: widget.width,
      height: widget.height,
      child: GestureDetector(
        behavior: HitTestBehavior.deferToChild,
        onPanDown: (DragDownDetails details) {
          double dx = details.localPosition.dx - _parentW / 2;
          double dy = details.localPosition.dy - _parentW / 2;
          _moveTo(dx, dy);
        },
        onPanEnd: (DragEndDetails details) {
          if (widget.recoverFunction == null || widget.recoverFunction!()) {
            _moveTo(0, 0);
          }
        },
        onPanUpdate: (DragUpdateDetails details) {
          double dx = details.localPosition.dx - _parentW / 2;
          double dy = details.localPosition.dy - _parentW / 2;
          _moveTo(dx, dy);
        },
        child: Container(
          decoration: _buildBackground(),
          child: Transform.translate(
            offset: Offset(_alightX, _alightY),
            child: Container(
              width: widget.width,
              alignment: const Alignment(0, 0),
              child: Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(widget.controlImg),
                  ),
                  //color: Colors.amberAccent,
                  shape: BoxShape.circle,
                ),
                width: _parentW * widget.radio,
                height: _parentW * widget.radio,
              ),
            ),
          ),
        ),
      ),
    );
  }

  //背景
  BoxDecoration _buildBackground() {
    return BoxDecoration(
      color: Colors.black,
      image: DecorationImage(
        image: AssetImage(_backgroundImage),
      ),
      boxShadow: const [
        BoxShadow(
          color: Colors.transparent,
          offset: Offset(-2.0, 3.0),
          blurRadius: 20,
          spreadRadius: 0.5,
        ),
      ],
      shape: BoxShape.circle,
    );
  }
}
